package com.zjut.an.config.action;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zjut.an.config.dao.AnConfig;
import com.zjut.an.config.manager.AnConfigManager;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.tools.JSONUtilClass;

public class AnConfigAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, ModelDriven<AnConfig>{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private AnConfig obj;
	private Map<String , Object> dataMap;
	private int page = 1, limit = 10;
	private String order;
	private String sort;
	private String search;
	private String rowstr;
	
	@Resource(name = "AnConfigManager")
	private AnConfigManager manager;
	
	public String anConfigList() throws UnsupportedEncodingException {
		try {
			if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
				request.setCharacterEncoding("UTF-8");
				dataMap = new HashMap<String, Object>();
				//int pageNumber = offset/limit + 1;
				
				PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
				System.out.println("@@@@@@@@@@ "+module.getObjects().size());
				dataMap.put("rows", module.getObjects());
				dataMap.put("total", module.getTotal()); 
				obj = null; 
			}			
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String anConfigDeletes() {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			String objumniJSON = rowstr;
			manager.deleteDatas(JSONUtilClass.getDTOList(objumniJSON, AnConfig.class));
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
		}		
		return SUCCESS;
	}
	
	public String anConfigAdd() {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			String objumniJSON = rowstr;
			obj = (AnConfig)JSONUtilClass.getDTO(objumniJSON, AnConfig.class);
			manager.addData(obj);
		}		
		return SUCCESS;
	}
	
	public String anConfigDelete() {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			String objumniJSON = rowstr;
			manager.deleteData((AnConfig)JSONUtilClass.getDTO(objumniJSON, AnConfig.class));
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
		}		
		return SUCCESS;
	}
	
	public String anConfigUpdate() {
		if (session.getAttribute("admin")!=null) {//只有管理员才允许执行该操作，以免信息泄露
			String objumniJSON = rowstr;
			manager.updateData((AnConfig)JSONUtilClass.getDTO(objumniJSON, AnConfig.class));
		}		
		return SUCCESS;
	}
	
	public String anConfigSave() {
		obj = (AnConfig)(JSONUtilClass.getDTOArray(rowstr, AnConfig.class)[0]);
		if (obj.getCoId()!=null) {
			//-------update-------
			manager.updateData(obj);
		} else {
			//-------insert-------
			manager.addData(obj);
		}
		rowstr = null;
		return SUCCESS;
	}
	
	@Override
	public AnConfig getModel() {
		if (null == obj) {
			obj = new AnConfig();
		}
		return obj;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public AnConfig getAl() {
		return obj;
	}

	public void setAl(AnConfig obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public AnConfigManager getManager() {
		return manager;
	}

	public void setManager(AnConfigManager manager) {
		this.manager = manager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

}
